﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.InfoCardSchemas
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;

namespace Microsoft.InfoCards
{
  internal class InfoCardSchemas
  {
    private static readonly string[] s_primarySchemas = new string[2]
    {
      "identity.xsd",
      "ws-identity-2007.xsd"
    };
    private static readonly InfoCardSchemas.SchemaLocationResourceMapping[] s_supportingSchemas = new InfoCardSchemas.SchemaLocationResourceMapping[13]
    {
      new InfoCardSchemas.SchemaLocationResourceMapping("addressing.xsd", "http://www.w3.org/2005/08/addressing/addressing.xsd"),
      new InfoCardSchemas.SchemaLocationResourceMapping("addressing04.xsd", "http://schemas.xmlsoap.org/ws/2004/08/addressing/"),
      new InfoCardSchemas.SchemaLocationResourceMapping("xmldsig-core-schema.xsd", "http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd"),
      new InfoCardSchemas.SchemaLocationResourceMapping("xml.xsd", "http://www.w3.org/2001/xml.xsd"),
      new InfoCardSchemas.SchemaLocationResourceMapping("xenc-schema.xsd", "http://www.w3.org/TR/xmlenc-core/xenc-schema.xsd"),
      new InfoCardSchemas.SchemaLocationResourceMapping("oasis-200401-wss-wssecurity-secext-1.0.xsd", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"),
      new InfoCardSchemas.SchemaLocationResourceMapping("oasis-200401-wss-wssecurity-utility-1.0.xsd", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"),
      new InfoCardSchemas.SchemaLocationResourceMapping(XmlNames.WSPolicyXmlSoap2004.Instance.LocalSchemaLocation, XmlNames.WSPolicyXmlSoap2004.Instance.SchemaLocation),
      new InfoCardSchemas.SchemaLocationResourceMapping(XmlNames.WSPolicyW32007.Instance.LocalSchemaLocation, XmlNames.WSPolicyW32007.Instance.SchemaLocation),
      new InfoCardSchemas.SchemaLocationResourceMapping(XmlNames.WSSecurityPolicyXmlSoap2005.Instance.LocalSchemaLocation, XmlNames.WSSecurityPolicyXmlSoap2005.Instance.SchemaLocation),
      new InfoCardSchemas.SchemaLocationResourceMapping(XmlNames.WSSecurityPolicyOasis2007.Instance.LocalSchemaLocation, XmlNames.WSSecurityPolicyOasis2007.Instance.SchemaLocation),
      new InfoCardSchemas.SchemaLocationResourceMapping(XmlNames.WSTrustXmlSoap2005.Instance.LocalSchemaLocation, XmlNames.WSTrustXmlSoap2005.Instance.SchemaLocation),
      new InfoCardSchemas.SchemaLocationResourceMapping(XmlNames.WSTrustOasis2007.Instance.LocalSchemaLocation, XmlNames.WSTrustOasis2007.Instance.SchemaLocation)
    };
    private static InfoCardSchemas s_current = new InfoCardSchemas(Assembly.GetExecutingAssembly());
    private InfoCardSchemas.LocalCachingResolver m_resolver;
    private Assembly m_assembly;
    private XmlSchemaSet m_schemas;
    private object m_sync;

    private InfoCardSchemas(Assembly sourceAssembly)
    {
      this.m_sync = new object();
      this.m_resolver = new InfoCardSchemas.LocalCachingResolver();
      this.m_assembly = sourceAssembly;
    }

    public static XmlSchemaSet GetSchemaSet()
    {
      if (InfoCardSchemas.s_current.m_schemas == null)
      {
        lock (InfoCardSchemas.s_current.m_sync)
        {
          if (InfoCardSchemas.s_current.m_schemas == null)
            InfoCardSchemas.s_current.InitializeSchemas();
        }
      }
      return InfoCardSchemas.s_current.m_schemas;
    }

    public static XmlReader CreateReader(string fragment)
    {
      return InfoCardSchemas.CreateReader(Utility.CreateReaderWithQuotas(fragment));
    }

    public static XmlReader CreateReader(Stream stream, XmlReaderSettings settings)
    {
      return InfoCardSchemas.CreateReader(Utility.CreateReaderWithQuotas(stream), settings);
    }

    public static XmlReader CreateReader(
      XmlDictionaryReader reader,
      XmlReaderSettings settings)
    {
      return XmlReader.Create((XmlReader) reader, settings);
    }

    public static XmlReader CreateReader(XmlDictionaryReader reader)
    {
      XmlReaderSettings defaultReaderSettings = InfoCardSchemas.CreateDefaultReaderSettings();
      return XmlReader.Create((XmlReader) reader, defaultReaderSettings);
    }

    public static XmlReaderSettings CreateDefaultReaderSettings()
    {
      XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
      xmlReaderSettings.CloseInput = false;
      xmlReaderSettings.ValidationType = ValidationType.Schema;
      xmlReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
      xmlReaderSettings.ConformanceLevel = ConformanceLevel.Auto;
      xmlReaderSettings.Schemas = InfoCardSchemas.GetSchemaSet();
      xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(InfoCardSchemas.ErrorHandler);
      xmlReaderSettings.XmlResolver = (XmlResolver) InfoCardSchemas.s_current.m_resolver;
      xmlReaderSettings.IgnoreComments = true;
      xmlReaderSettings.IgnoreWhitespace = true;
      xmlReaderSettings.IgnoreProcessingInstructions = true;
      return xmlReaderSettings;
    }

    private void InitializeSchemas()
    {
      InfoCardSchemas.LocalCachingResolver localCachingResolver = new InfoCardSchemas.LocalCachingResolver();
      for (int index = 0; index < InfoCardSchemas.s_supportingSchemas.Length; ++index)
      {
        Uri location = new Uri(InfoCardSchemas.s_supportingSchemas[index].SchemaLocation);
        System.Xml.Schema.XmlSchema schema = System.Xml.Schema.XmlSchema.Read(this.m_assembly.GetManifestResourceStream(InfoCardSchemas.s_supportingSchemas[index].ResourceName), new ValidationEventHandler(InfoCardSchemas.ErrorHandler));
        localCachingResolver.AddLocalSchema(location, schema);
      }
      XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
      xmlSchemaSet.XmlResolver = (XmlResolver) localCachingResolver;
      for (int index = 0; index < InfoCardSchemas.s_primarySchemas.Length; ++index)
      {
        System.Xml.Schema.XmlSchema schema = System.Xml.Schema.XmlSchema.Read(this.m_assembly.GetManifestResourceStream(InfoCardSchemas.s_primarySchemas[index]), new ValidationEventHandler(InfoCardSchemas.ErrorHandler));
        xmlSchemaSet.Add(schema);
      }
      xmlSchemaSet.ValidationEventHandler += new ValidationEventHandler(InfoCardSchemas.ErrorHandler);
      xmlSchemaSet.Compile();
      this.m_resolver = localCachingResolver;
      this.m_schemas = xmlSchemaSet;
    }

    private static void ErrorHandler(object sender, ValidationEventArgs args)
    {
      if (args.Severity == XmlSeverityType.Error)
        throw InfoCardTrace.ThrowHelperCritical((Exception) new XmlSchemaValidationException(SR.GetString("SchemaValidationError", (object) args.Exception.Message), (Exception) args.Exception));
    }

    private class LocalCachingResolver : XmlUrlResolver
    {
      private Dictionary<Uri, System.Xml.Schema.XmlSchema> m_table;

      public LocalCachingResolver()
      {
        this.m_table = new Dictionary<Uri, System.Xml.Schema.XmlSchema>();
      }

      public void AddLocalSchema(Uri location, System.Xml.Schema.XmlSchema schema)
      {
        this.m_table.Add(location, schema);
      }

      public override Uri ResolveUri(Uri baseUri, string relativeUri)
      {
        return base.ResolveUri(baseUri, relativeUri);
      }

      public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
      {
        return (object) this.m_table[absoluteUri];
      }
    }

    private class SchemaLocationResourceMapping
    {
      private string m_resourceName;
      private string m_schemaLocation;

      public SchemaLocationResourceMapping(string resourceName, string schemaLocation)
      {
        this.m_resourceName = resourceName;
        this.m_schemaLocation = schemaLocation;
      }

      public string ResourceName
      {
        get
        {
          return this.m_resourceName;
        }
      }

      public string SchemaLocation
      {
        get
        {
          return this.m_schemaLocation;
        }
      }
    }
  }
}
